#pragma once
#include "ast.h"
#include <vector>
#include <string>
#include <unordered_map>
#include <stdexcept>

class SemanticError : public std::runtime_error {
public:
    using std::runtime_error::runtime_error;
};

//enum class Type { TInt, TBool, TVoid, TFunc };
enum class Type { TInt, TBool, TVoid, TFunc, TConvertible };

class SymbolTable {
    std::vector<std::unordered_map<std::string, Type>> scopes;
    
public:
    void enterScope();
    void exitScope();
    void addSymbol(const std::string& id, Type type);
    Type lookup(const std::string& id) const;
};

class SemanticAnalyzer {
    SymbolTable symbolTable;
    Type currentReturnType;
    bool inLoop = false;
    
    Type checkExpr(Expr* expr);
    void checkStmt(Stmt* stmt);
    void checkFunc(FuncDef* func);
    
public:
    void checkCompUnit(CompUnit* cu);
};